`
https://leetcode.cn/problems/sum-root-to-leaf-numbers/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumNumbers = function (root) {
  let sum = 0
  const track = []

  const traverse = (root) => {
    // base case
    if (root === null) return

    // 将当前值加入路径
    track.push(root.val)

    // 如果当前结点是叶子，则计算出数字并累加到结果上
    if (root.left === null && root.right === null) {
      sum += +track.join('')
    }

    // 递归遍历
    traverse(root.left)
    traverse(root.right)

    // 把当前结点值从路径中取出
    track.pop()
  }

  traverse(root)

  return sum
};